
import DataTableUtils from '@/utils/DataTableUtils'

import {Tree} from 'element-ui'

export default {
  mixins: [DataTableUtils],
  components: {Tree},
  data() {
    return {
      leftWidth: 250,
      //
      dataTree: null,
      dataTreeQueryUrl: '',
      dataTreeSelected: null,
      dataTreeLoading: false,
      queryDataTree: window.$debounce(this._queryDataTree),
      dataTreeClickCount: 0,
      doDataTreeDblClick: window.$debounce(this._doDataTreeDblClick, 250),
    }
  },
  computed: {
    rightWidth() {
      return this.pageWidth - this.leftWidth
    }
  },
  watch: {
    dataTreeQueryUrl() {
      this.queryDataTree();
    },
  },
  methods: {
    init() {
      this.queryDataTree()
      this.queryDataRows()
    },
    _queryDataTree() {
      if (!this.dataTreeQueryUrl) {
        return
      }
      this.dataTreeLoading = true
      this.$http.get(this.dataTreeQueryUrl).then((rs) => {
        this.dataTreeLoading = false
        const result = rs.data
        if (result.success) {
          this.dataTree = result.data
        } else {
          this.dataTree = []
        }
      })
    },
    _doDataTreeDblClick(data) {
      // 处理单击
      this.dataTreeSelected = data
      this.queryDataRows()
      // 处理双击
      if (this.dataTreeClickCount >= 2) {
        const node = this.$refs.tree.store.nodesMap[data.id]
        node.expanded = !node.expanded
      }
      this.dataTreeClickCount = 0;
    },
    doDataTreeSelect(data) {
      this.dataTreeClickCount++;
      this.doDataTreeDblClick(data)
    },
    doDataTreeUnselect() {
      this.$refs.tree.setCurrentKey(null)
      this.doDataTreeSelect(null)
    }
  }
}